﻿using Dolany.UtilityTool;
using JetBrains.Annotations;
using Missbot.AIModule.Base;
using Missbot.Core.Base;
using Missbot.Core.Cache;
using Missbot.Model.MsgCmds.Notes;

namespace Missbot.AIModule.AutoReaction
{
    [UsedImplicitly]
    public class AutoReactionMod : IAIModule
    {
        public override string Name => "自动点赞";
        public override AIPriority Priority => AIPriority.High;
        public override string ParentTag => "嘟嘟互动";

        private readonly string[] Reactions = { "⭐", "❤", "🎉", "👍", "😊" };

        [ModuleEnter(Command = "开启自动点赞功能", Description = "开启嘟文自动点赞功能")]
        public bool EnableAutoReaction(InboxSession session)
        {
            if (session.User == null)
            {
                return false;
            }

            var rec = AutoReactionRecDA.Get(session.User.ID);
            if (rec != null)
            {
                session.SendBack("你已经开启自动点赞功能啦！");
                return true;
            }

            rec = new AutoReactionRec { UserID = session.User.ID, UserName = session.User.UserName, Host = session.User.Host };
            rec.Insert();
            session.SendBack("自动点赞功能已经成功开启！");
            return true;
        }

        protected override bool OnInboxImpl(InboxSession session)
        {
            if (session.Channel.BindingType != ChannelType.Note)
            {
                return base.OnInboxImpl(session);
            }

            if (string.IsNullOrEmpty(session.Text))
            {
                return base.OnInboxImpl(session);
            }

            var rec = AutoReactionRecDA.Get(session.User.ID);
            if (rec != null)
            {
                MsgSender.PushMsg(new CreateReactionCmd { NoteId = session.SourceID, Reaction = Reactions.RandElement() });
            }

            return base.OnInboxImpl(session);
        }
    }
}